{
 "cells": [
  {
   "cell_type": "code",
   "id": "f5e67ed4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-23T12:28:58.764878Z",
     "start_time": "2025-05-23T12:28:58.197773Z"
    }
   },
   "source": [
    "import torch\n",
    "from torch import nn\n",
    "from torchviz import make_dot\n",
    "import os\n",
    "os.environ[\"PATH\"] += os.pathsep + 'C:/Program Files/Graphviz/bin'\n",
    "from torch.onnx import TrainingMode # 导出 ONNX 模型时设置模式为 EVAL\n",
    "\n",
    "# 生成一个形状为1*8的随机张量\n",
    "x = torch.randn(1,8)\n",
    "# 创建输入和模型\n",
    "model = nn.Sequential(\n",
    "    nn.Linear(8, 16),\n",
    "    nn.Tanh(),\n",
    "    nn.Linear(16, 1)\n",
    ")\n",
    "\n",
    "# 使用make_dot函数针对x生成过程计算图\n",
    "vis_graph = make_dot(model(x), params=dict(model.named_parameters()))\n",
    "# 查看生成的图\n",
    "vis_graph.view()\n",
    "#  生成ONNX文件\n",
    "with torch.onnx.select_model_mode_for_export(model, TrainingMode.EVAL):\n",
    "    # 使用torch.jit.trace记录模型的计算过程\n",
    "    trace= torch.jit.trace(model, (x,))\n",
    "    output = trace(x)\n",
    "print(output)\n",
    "\n",
    "display(vis_graph)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[-0.0280]], grad_fn=<AddmmBackward0>)\n"
     ]
    },
    {
     "data": {
      "image/svg+xml": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Generated by graphviz version 12.2.1 (20241206.2353)\n -->\n<!-- Pages: 1 -->\n<svg width=\"337pt\" height=\"432pt\"\n viewBox=\"0.00 0.00 337.00 432.25\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 428.25)\">\n<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-428.25 333,-428.25 333,4 -4,4\"/>\n<!-- 3023463043296 -->\n<g id=\"node1\" class=\"node\">\n<title>3023463043296</title>\n<polygon fill=\"#caff70\" stroke=\"black\" points=\"194,-32.75 136,-32.75 136,0 194,0 194,-32.75\"/>\n<text text-anchor=\"middle\" x=\"165\" y=\"-7.25\" font-family=\"monospace\" font-size=\"10.00\"> (1, 1)</text>\n</g>\n<!-- 3023462991952 -->\n<g id=\"node2\" class=\"node\">\n<title>3023462991952</title>\n<polygon fill=\"lightgrey\" stroke=\"black\" points=\"215,-89.5 115,-89.5 115,-68.75 215,-68.75 215,-89.5\"/>\n<text text-anchor=\"middle\" x=\"165\" y=\"-76\" font-family=\"monospace\" font-size=\"10.00\">AddmmBackward0</text>\n</g>\n<!-- 3023462991952&#45;&gt;3023463043296 -->\n<g id=\"edge13\" class=\"edge\">\n<title>3023462991952&#45;&gt;3023463043296</title>\n<path fill=\"none\" stroke=\"black\" d=\"M165,-68.36C165,-61.89 165,-53.05 165,-44.55\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"168.5,-44.55 165,-34.55 161.5,-44.55 168.5,-44.55\"/>\n</g>\n<!-- 3023462992384 -->\n<g id=\"node3\" class=\"node\">\n<title>3023462992384</title>\n<polygon fill=\"lightgrey\" stroke=\"black\" points=\"100,-146.25 0,-146.25 0,-125.5 100,-125.5 100,-146.25\"/>\n<text text-anchor=\"middle\" x=\"50\" y=\"-132.75\" font-family=\"monospace\" font-size=\"10.00\">AccumulateGrad</text>\n</g>\n<!-- 3023462992384&#45;&gt;3023462991952 -->\n<g id=\"edge1\" class=\"edge\">\n<title>3023462992384&#45;&gt;3023462991952</title>\n<path fill=\"none\" stroke=\"black\" d=\"M70.56,-125.09C88.3,-116.64 114.18,-104.32 134.37,-94.71\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"135.73,-97.94 143.25,-90.48 132.72,-91.62 135.73,-97.94\"/>\n</g>\n<!-- 3023463043216 -->\n<g id=\"node4\" class=\"node\">\n<title>3023463043216</title>\n<polygon fill=\"lightblue\" stroke=\"black\" points=\"77,-215.75 23,-215.75 23,-182.25 77,-182.25 77,-215.75\"/>\n<text text-anchor=\"middle\" x=\"50\" y=\"-202.25\" font-family=\"monospace\" font-size=\"10.00\">2.bias</text>\n<text text-anchor=\"middle\" x=\"50\" y=\"-189.5\" font-family=\"monospace\" font-size=\"10.00\"> (1)</text>\n</g>\n<!-- 3023463043216&#45;&gt;3023462992384 -->\n<g id=\"edge2\" class=\"edge\">\n<title>3023463043216&#45;&gt;3023462992384</title>\n<path fill=\"none\" stroke=\"black\" d=\"M50,-181.94C50,-174.6 50,-165.82 50,-157.96\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"53.5,-158.23 50,-148.23 46.5,-158.23 53.5,-158.23\"/>\n</g>\n<!-- 3023462993632 -->\n<g id=\"node5\" class=\"node\">\n<title>3023462993632</title>\n<polygon fill=\"lightgrey\" stroke=\"black\" points=\"212,-146.25 118,-146.25 118,-125.5 212,-125.5 212,-146.25\"/>\n<text text-anchor=\"middle\" x=\"165\" y=\"-132.75\" font-family=\"monospace\" font-size=\"10.00\">TanhBackward0</text>\n</g>\n<!-- 3023462993632&#45;&gt;3023462991952 -->\n<g id=\"edge3\" class=\"edge\">\n<title>3023462993632&#45;&gt;3023462991952</title>\n<path fill=\"none\" stroke=\"black\" d=\"M165,-125.09C165,-118.47 165,-109.47 165,-101.27\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"168.5,-101.34 165,-91.34 161.5,-101.34 168.5,-101.34\"/>\n</g>\n<!-- 3023446203792 -->\n<g id=\"node6\" class=\"node\">\n<title>3023446203792</title>\n<polygon fill=\"lightgrey\" stroke=\"black\" points=\"211,-209.38 111,-209.38 111,-188.62 211,-188.62 211,-209.38\"/>\n<text text-anchor=\"middle\" x=\"161\" y=\"-195.88\" font-family=\"monospace\" font-size=\"10.00\">AddmmBackward0</text>\n</g>\n<!-- 3023446203792&#45;&gt;3023462993632 -->\n<g id=\"edge4\" class=\"edge\">\n<title>3023446203792&#45;&gt;3023462993632</title>\n<path fill=\"none\" stroke=\"black\" d=\"M161.64,-188.18C162.18,-180.02 162.95,-168.13 163.63,-157.85\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"167.11,-158.19 164.27,-147.98 160.13,-157.73 167.11,-158.19\"/>\n</g>\n<!-- 3023446204032 -->\n<g id=\"node7\" class=\"node\">\n<title>3023446204032</title>\n<polygon fill=\"lightgrey\" stroke=\"black\" points=\"120,-278.88 20,-278.88 20,-258.12 120,-258.12 120,-278.88\"/>\n<text text-anchor=\"middle\" x=\"70\" y=\"-265.38\" font-family=\"monospace\" font-size=\"10.00\">AccumulateGrad</text>\n</g>\n<!-- 3023446204032&#45;&gt;3023446203792 -->\n<g id=\"edge5\" class=\"edge\">\n<title>3023446204032&#45;&gt;3023446203792</title>\n<path fill=\"none\" stroke=\"black\" d=\"M83.04,-257.83C97.55,-247.06 121.26,-229.48 138.73,-216.52\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"140.53,-219.54 146.47,-210.77 136.36,-213.92 140.53,-219.54\"/>\n</g>\n<!-- 3023463043696 -->\n<g id=\"node8\" class=\"node\">\n<title>3023463043696</title>\n<polygon fill=\"lightblue\" stroke=\"black\" points=\"97,-354.75 43,-354.75 43,-321.25 97,-321.25 97,-354.75\"/>\n<text text-anchor=\"middle\" x=\"70\" y=\"-341.25\" font-family=\"monospace\" font-size=\"10.00\">0.bias</text>\n<text text-anchor=\"middle\" x=\"70\" y=\"-328.5\" font-family=\"monospace\" font-size=\"10.00\"> (16)</text>\n</g>\n<!-- 3023463043696&#45;&gt;3023446204032 -->\n<g id=\"edge6\" class=\"edge\">\n<title>3023463043696&#45;&gt;3023446204032</title>\n<path fill=\"none\" stroke=\"black\" d=\"M70,-321C70,-311.9 70,-300.39 70,-290.57\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"73.5,-290.84 70,-280.84 66.5,-290.84 73.5,-290.84\"/>\n</g>\n<!-- 3023446203984 -->\n<g id=\"node9\" class=\"node\">\n<title>3023446203984</title>\n<polygon fill=\"lightgrey\" stroke=\"black\" points=\"214,-278.88 138,-278.88 138,-258.12 214,-258.12 214,-278.88\"/>\n<text text-anchor=\"middle\" x=\"176\" y=\"-265.38\" font-family=\"monospace\" font-size=\"10.00\">TBackward0</text>\n</g>\n<!-- 3023446203984&#45;&gt;3023446203792 -->\n<g id=\"edge7\" class=\"edge\">\n<title>3023446203984&#45;&gt;3023446203792</title>\n<path fill=\"none\" stroke=\"black\" d=\"M173.85,-257.83C171.71,-248.21 168.36,-233.13 165.62,-220.79\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"169.06,-220.13 163.47,-211.13 162.23,-221.65 169.06,-220.13\"/>\n</g>\n<!-- 3023446203120 -->\n<g id=\"node10\" class=\"node\">\n<title>3023446203120</title>\n<polygon fill=\"lightgrey\" stroke=\"black\" points=\"226,-348.38 126,-348.38 126,-327.62 226,-327.62 226,-348.38\"/>\n<text text-anchor=\"middle\" x=\"176\" y=\"-334.88\" font-family=\"monospace\" font-size=\"10.00\">AccumulateGrad</text>\n</g>\n<!-- 3023446203120&#45;&gt;3023446203984 -->\n<g id=\"edge8\" class=\"edge\">\n<title>3023446203120&#45;&gt;3023446203984</title>\n<path fill=\"none\" stroke=\"black\" d=\"M176,-327.33C176,-317.71 176,-302.63 176,-290.29\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"179.5,-290.67 176,-280.67 172.5,-290.67 179.5,-290.67\"/>\n</g>\n<!-- 3023463043456 -->\n<g id=\"node11\" class=\"node\">\n<title>3023463043456</title>\n<polygon fill=\"lightblue\" stroke=\"black\" points=\"208,-424.25 144,-424.25 144,-390.75 208,-390.75 208,-424.25\"/>\n<text text-anchor=\"middle\" x=\"176\" y=\"-410.75\" font-family=\"monospace\" font-size=\"10.00\">0.weight</text>\n<text text-anchor=\"middle\" x=\"176\" y=\"-398\" font-family=\"monospace\" font-size=\"10.00\"> (16, 8)</text>\n</g>\n<!-- 3023463043456&#45;&gt;3023446203120 -->\n<g id=\"edge9\" class=\"edge\">\n<title>3023463043456&#45;&gt;3023446203120</title>\n<path fill=\"none\" stroke=\"black\" d=\"M176,-390.5C176,-381.4 176,-369.89 176,-360.07\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"179.5,-360.34 176,-350.34 172.5,-360.34 179.5,-360.34\"/>\n</g>\n<!-- 3023462992720 -->\n<g id=\"node12\" class=\"node\">\n<title>3023462992720</title>\n<polygon fill=\"lightgrey\" stroke=\"black\" points=\"310,-146.25 234,-146.25 234,-125.5 310,-125.5 310,-146.25\"/>\n<text text-anchor=\"middle\" x=\"272\" y=\"-132.75\" font-family=\"monospace\" font-size=\"10.00\">TBackward0</text>\n</g>\n<!-- 3023462992720&#45;&gt;3023462991952 -->\n<g id=\"edge10\" class=\"edge\">\n<title>3023462992720&#45;&gt;3023462991952</title>\n<path fill=\"none\" stroke=\"black\" d=\"M252.87,-125.09C236.51,-116.72 212.72,-104.54 194.02,-94.97\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"195.81,-91.96 185.31,-90.52 192.62,-98.19 195.81,-91.96\"/>\n</g>\n<!-- 3023446202928 -->\n<g id=\"node13\" class=\"node\">\n<title>3023446202928</title>\n<polygon fill=\"lightgrey\" stroke=\"black\" points=\"329,-209.38 229,-209.38 229,-188.62 329,-188.62 329,-209.38\"/>\n<text text-anchor=\"middle\" x=\"279\" y=\"-195.88\" font-family=\"monospace\" font-size=\"10.00\">AccumulateGrad</text>\n</g>\n<!-- 3023446202928&#45;&gt;3023462992720 -->\n<g id=\"edge11\" class=\"edge\">\n<title>3023446202928&#45;&gt;3023462992720</title>\n<path fill=\"none\" stroke=\"black\" d=\"M277.88,-188.18C276.94,-180.02 275.58,-168.13 274.4,-157.85\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"277.89,-157.51 273.27,-147.97 270.93,-158.31 277.89,-157.51\"/>\n</g>\n<!-- 3023445527984 -->\n<g id=\"node14\" class=\"node\">\n<title>3023445527984</title>\n<polygon fill=\"lightblue\" stroke=\"black\" points=\"311,-285.25 247,-285.25 247,-251.75 311,-251.75 311,-285.25\"/>\n<text text-anchor=\"middle\" x=\"279\" y=\"-271.75\" font-family=\"monospace\" font-size=\"10.00\">2.weight</text>\n<text text-anchor=\"middle\" x=\"279\" y=\"-259\" font-family=\"monospace\" font-size=\"10.00\"> (1, 16)</text>\n</g>\n<!-- 3023445527984&#45;&gt;3023446202928 -->\n<g id=\"edge12\" class=\"edge\">\n<title>3023445527984&#45;&gt;3023446202928</title>\n<path fill=\"none\" stroke=\"black\" d=\"M279,-251.5C279,-242.4 279,-230.89 279,-221.07\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"282.5,-221.34 279,-211.34 275.5,-221.34 282.5,-221.34\"/>\n</g>\n</g>\n</svg>\n",
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x2bff4700700>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "execution_count": 6
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pytorch_stu",
   "language": "python",
   "name": "pytorch_stu"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
